﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace TaskQuqueDemo
{
    public class TestParallelTask
    {
        public void Test1()
        {
            var time1 = DateTime.Now;
            Console.WriteLine(time1);
            Console.WriteLine("开始执行任务...");
            var tasks = new List<Action>();
            tasks.Add(() => Task.Run(() => { TestTask1(); }).Wait());
            tasks.Add(() => Task.Run(() => { TestTask2("task2input"); }).Wait());
            tasks.Add(() => Task.Run(() => { TestTask3("task3input").Wait(); }).Wait());
            Parallel.ForEach(tasks, task => task());
            var time2 = DateTime.Now;
            Console.WriteLine(time2);
            
            Console.WriteLine($"所有任务执行完成...耗时{(time2 - time1).TotalSeconds}秒");
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public Task TestTask1()
        {
            Console.WriteLine("任务1开始执行...");
            Thread.Sleep(1000);
            //执行业务代码    
            Console.WriteLine("任务1执行完成...");
            return Task.CompletedTask;
        }

        public Task TestTask2(string input)
        {

            Console.WriteLine($"任务2开始执行...input{input}");
            Thread.Sleep(2000);
            //执行业务代码
            Console.WriteLine("任务2执行完成...");
            return Task.CompletedTask;
        }

        public async Task TestTask3(string input)
        {
            Console.WriteLine($"任务3开始执行...input{input}");
            Thread.Sleep(3000);
            //执行业务代码
            Console.WriteLine("任务3执行完成...");
            await Task.CompletedTask;

        }
    }
}
